home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / util / libs / shutdown.lha / shutdown_5.3 / src / testfs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-12  |  4.1 KB  |  206 lines

  1. /*
  2.    testfs.c --- test (filesystem) handlers.
  3.  
  4.    (c) Copyright 1998 SHW Wabnitz
  5.    Written by Bernhard Fastenrath (fasten@shw.com)
  6.  
  7.    This file may be distributed under the terms
  8.    of the GNU General Public License.
  9. */
  10.  
  11. #include <stdio.h>
  12.  
  13. #include "shutdown_cmd.h"
  14.  
  15. typedef struct {
  16.   struct Message   sp_Msg;
  17.   struct DosPacket sp_Pkt;
  18.   struct Node     *sp_Dev;
  19. } MyStandardPacket;
  20.  
  21. /// FreeList()
  22. static void
  23. FreeList (List *list)
  24. {
  25.   Node *node, *next;
  26.  
  27.   for (next = node = list -> lh_Head; next = node -> ln_Succ; node = next)
  28.   {
  29.     if (node -> ln_Name)
  30.       FreeMem (node -> ln_Name, node -> ln_Pri);
  31.     FreeMem (node, sizeof (Node));
  32.   }
  33. }
  34. ///
  35.  
  36. /// FreeStdPktList()
  37. static void
  38. FreeStdPktList (List *list)
  39. {
  40.   Node *node, *next;
  41.  
  42.   for (next = node = list -> lh_Head; next = node -> ln_Succ; node = next)
  43.     FreeMem (node, sizeof (MyStandardPacket));
  44. }
  45. ///
  46.  
  47. /// SendStandardPacket()
  48. static int
  49. SendStandardPacket (MsgPort *mp, MyStandardPacket *sp, char *handler, ULONG action, ULONG arg1)
  50. {
  51.   DevProc *dp = NULL;
  52.  
  53.   dp = GetDeviceProc (handler, dp);
  54.   if (dp)
  55.   {
  56.     sp -> sp_Pkt.dp_Type = action;
  57.     sp -> sp_Pkt.dp_Arg1 = arg1;
  58.     SendPkt (&sp -> sp_Pkt, dp -> dvp_Port, mp);
  59.     FreeDeviceProc (dp);
  60.     return 1;
  61.   }
  62.   return 0;
  63. }
  64. ///
  65.  
  66. /// AllocateStandardPackets ()
  67. static int
  68. AllocateStandardPackets (List *pktlist, int count)
  69. {
  70.   MyStandardPacket *sp;  
  71.   int t;
  72.  
  73.   NewList (pktlist);
  74.  
  75.   for (t=0; t<count; t++)
  76.   {
  77.     if (!(sp = AllocMem (sizeof (MyStandardPacket), MEMF_PUBLIC | MEMF_CLEAR)))
  78.       break;
  79.     sp -> sp_Msg.mn_Length = sizeof (MyStandardPacket);
  80.     sp -> sp_Msg.mn_Node.ln_Name = (char *) &sp -> sp_Pkt;
  81.     sp -> sp_Pkt.dp_Link = &sp -> sp_Msg;
  82.     AddTail (pktlist, &sp -> sp_Msg.mn_Node);
  83.   }
  84.   return t;
  85. }
  86. ///
  87.  
  88. /// TestHandler()
  89. static int
  90. TestHandler (MsgPort *mp, char *filesystem, int flag)
  91. {
  92.   int len, count = 0, icount = 0, out_of_mem = 0;
  93.   List devlist, pktlist;
  94.   MyStandardPacket *sp;
  95.   Node *node;
  96.   DosList *dl;
  97.   char *name;
  98.  
  99.   NewList (&devlist);
  100.   NewList (&pktlist);
  101.  
  102.   dl = LockDosList ( LDF_DEVICES | LDF_READ );
  103.  
  104.   while (dl = NextDosEntry (dl, LDF_DEVICES))
  105.   {
  106.     if (node = (Node *) AllocMem (sizeof (Node), MEMF_CLEAR))
  107.     {
  108.       name = BADDR (dl -> dol_Name);
  109.       len = (int) name[0];
  110.  
  111.       if (node -> ln_Name = AllocMem (len + 2, 0))
  112.       {
  113.         CopyMem (name+1, node -> ln_Name, len);
  114.         node -> ln_Name[len] = ':';
  115.         node -> ln_Name[len+1] = '\0';
  116.         node -> ln_Pri = len + 2;
  117.         AddTail (&devlist, node);
  118.         count ++;
  119.       }
  120.       else
  121.       {
  122.         out_of_mem = 1;
  123.         FreeMem (node, sizeof (Node));
  124.         break;
  125.       }
  126.     }
  127.   }
  128.  
  129.   UnLockDosList ( LDF_DEVICES | LDF_READ );
  130.  
  131.   if ((icount = AllocateStandardPackets (&pktlist, count)) == 0)
  132.     out_of_mem = 1;
  133.   count = icount;
  134.  
  135.   if (out_of_mem)
  136.   {
  137.     FreeStdPktList (&pktlist);
  138.     FreeList (&devlist);
  139.     return 0;
  140.   }
  141.  
  142.   node = devlist.lh_Head;
  143.  
  144.   printf ("testfs found the following handlers:\n");
  145.  
  146.   while (node -> ln_Succ)
  147.   {
  148.     if (sp = (MyStandardPacket *) RemHead (&pktlist))
  149.     {
  150.       sp -> sp_Dev = node;
  151.       SendStandardPacket (mp, sp, node -> ln_Name, ACTION_IS_FILESYSTEM, 0);
  152.       icount --;
  153.       printf ("%s ", node -> ln_Name);
  154.       node = node -> ln_Succ;
  155.     }
  156.   }
  157.   printf ("\n");
  158.   for (;;)
  159.   {
  160.     WaitPort (mp);
  161.  
  162.     while (sp = (MyStandardPacket *) GetMsg (mp))
  163.     {
  164.       icount ++;
  165.       node = (Node *) sp -> sp_Dev;
  166.  
  167.       if (sp -> sp_Pkt.dp_Type == ACTION_IS_FILESYSTEM &&
  168.           sp -> sp_Pkt.dp_Res1 == DOSTRUE)
  169.       {
  170.         printf ("Filesystem Handler: %s", node -> ln_Name);
  171.         AddTail (&pktlist, &sp -> sp_Msg.mn_Node);
  172.       }
  173.       else
  174.       {
  175.         printf ("    Non-fs Handler: %s", node -> ln_Name); 
  176.         AddTail (&pktlist, &sp -> sp_Msg.mn_Node);
  177.       }
  178.       printf ("\t(%d replies out of %d)\n", icount, count);
  179.     }
  180.     if (icount == count)
  181.       break;
  182.   }
  183.   FreeStdPktList (&pktlist);
  184.   FreeList (&devlist);
  185.   return 1;
  186. }
  187. ///
  188.  
  189. /// main()
  190. int
  191. main (int argc, char *argv[])
  192. {
  193.   char *filesystem = NULL;
  194.   MsgPort *mp;
  195.  
  196.   if (!(mp = CreatePort (NULL, 0)))
  197.     return 0;
  198.  
  199.   if (argc == 2) {
  200.     filesystem = argv[1];
  201.   }
  202.   TestHandler (mp, filesystem, 0);
  203.   DeletePort (mp);
  204. }
  205. ///
  206.